<?php

namespace app\api\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;
use think\response\Json;

class FeedBack extends AuthBase
{
    /**
     * 意见反馈
     * @return Json
     */
    public function save(): Json
    {
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $category_id = input("category_id", 0, "intval");
        $attachment = $this->request->param("attachment", "", "trim");
        $content = $this->request->param("content", "", "trim");

        //参数校验
        $data = [
            'category_id' => $category_id,
            'attachment' => $attachment,
            'content' => $content,
        ];
        $validate = new \app\api\validate\Feedback();
        if (!$validate->scene('save')->check($data)) {
            return Show::error([], $validate->getError());
        }

        try {
            $result = (new \app\common\business\FeedBack())->saveData($this->userId,$data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "保存成功");
        }
        return Show::error([], '保存失败');
    }

    //我的投诉建议分页列表
    public function index(){
        list($page,$limit) = Param::getMore([
            ['page', 1],
            ['limit', 10],
        ], $this->request, true);

        $feedBackBis = new \app\common\business\FeedBack();

        //筛选条件
        $where = [['user_id','=',$this->userId]];
        $list = $feedBackBis->getListByWhere($where,$page,$limit,'id,category_id,attachment,content,create_time');
        $totalCount = $feedBackBis->getCountByWhere($where,'id');
        if (!empty($list)) {$list = $feedBackBis->resetTextList($list);}//reset

        $result = [
            'count'=>$totalCount,
            'list'=>$list
        ];
        return Show::success($result);
    }

    public function detail()
    {
        $id = input("id", 0, "intval");

        $feedBackBis = new \app\common\business\FeedBack();

        //筛选条件
        $where = [
            ['id','=',$id],
            ['user_id','=',$this->userId]
        ];
        $info = $feedBackBis->getNormalByWhere($where);
        if (empty($info)) return Show::error([],'数据不存在');
        return Show::success($info);
    }
}
